/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

<template>
  <div class="transform-img">
    <img
      ref="imgSize"
      v-interaction="{
        handlers: {
          onPinchStart: imgPinchStart,
          onPinchMove: imgPinchMove,
          onPinchEnd: imgPinchEnd,
          onRotateStart: imgRotateStart,
          onRotateMove: imgRotateMove,
          onRotateEnd: imgRotateEnd,
        },
        configs: { enableScale: true, enableRotate: true, scaleDistance: 1 },
      }"
      src="../../assets/images/artwork.svg"
    />
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { PointerState } from "@hadss/web_uni_input";

const imgSize = ref<HTMLElement>(null);
const originalWidth = ref<number>(0);
const originalHeight = ref<number>(0);
const updateScale = ref<number | null>(null);
const updateRotate = ref<number | null>(null);
const isScaleOrRotate = ref<string>("scale");
const imgPinchStart = () => {
  isScaleOrRotate.value = "scale";
};
const imgPinchMove = (state: PointerState) => {
  const newWidth = originalWidth.value * state.scale;
  const newHeight = originalHeight.value * state.scale;
  if (updateScale.value) {
    const newScale = updateScale.value * state.scale;
    if (newScale > 0.6 && newScale < 1) {
      state.target.style.transform = `scale(${updateScale.value}) scale(${state.scale})`;
    } else if (newScale > 1) {
      state.target.style.transform = `scale(${1})`;
    } else if (newScale < 0.6) {
      state.target.style.transform = `scale(${0.6})`;
    }
  } else if (
    newWidth <= originalWidth.value &&
    newHeight <= originalHeight.value
  ) {
    if (state.scale > 0.6) {
      state.target.style.transform = `scale(${state.scale})`;
    } else {
      state.target.style.transform = `scale(${0.6})`;
    }
  }
};
const imgPinchEnd = (state: PointerState) => {
  updateScale.value = getMatrix(state, "scale");
};
const imgRotateStart = () => {
  isScaleOrRotate.value = "rotate";
};
const imgRotateMove = (state: PointerState) => {
  if (updateRotate.value) {
    state.target.style.transform = `rotate(${
      updateRotate.value + state.angle + "deg"
    })`;
  } else {
    state.target.style.transform = `rotate(${state.angle + "deg"})`;
  }
};
const imgRotateEnd = (state: PointerState) => {
  updateRotate.value = getMatrix(state, "rotate");
};

const getMatrix = (state: PointerState, str: string) => {
  const style = window.getComputedStyle(state.target);
  const matrix = style.transform;
  if (matrix === "none") {
    return null;
  }
  const values = matrix.match(/matrix\(([^)]+)\)/)[1].split(", ");
  const a = parseFloat(values[0]);
  const b = parseFloat(values[1]);
  const scale = Math.sqrt(a * a + b * b);
  const angle = Math.atan2(b, a) * (180 / Math.PI);
  if (str === "scale") {
    return Number(scale);
  } else {
    return angle;
  }
};

onMounted(() => {
  if (imgSize.value) {
    originalWidth.value = imgSize.value.offsetWidth;
    originalHeight.value = imgSize.value.offsetHeight;
  }
});
</script>

<style lang="less" scoped>
@import "./index.less";
</style>